package com.wang.system.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.common.result.Result;
import com.wang.common.result.ResultCodeEnum;
import com.wang.entity.model.SysTenantModel;
import com.wang.entity.system.SysTenant;
import com.wang.entity.vo.SysTenantVo;
import com.wang.system.service.system.SysTenantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @Author wang
 * @Date 2024/9/22 21:58
 * @PackageName:com.wang.system.controller.system
 * @ClassName: SysTenantController
 * @Description: TODO
 * @Version 1.0
 */
@RestController
@Api(tags = "系统租户管理")
@RequestMapping("/admin/system/tenant")
@RequiredArgsConstructor
public class SysTenantController {

    private final SysTenantService tenantService;
    @GetMapping(value = "/list")
    @ApiOperation(value = "租户列表", notes = "租户列表接口")
    public Result<IPage<SysTenantVo>> queryPageList(SysTenantModel sysTenantModel, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        Page<SysTenantVo> page = new Page<>(pageNo, pageSize);
        return Result.ok(tenantService.queryPageList(sysTenantModel, page));
    }

    // 禁用/启用租户
    @PutMapping(value = "/{tenantId}/status/{status}")
    @ApiOperation(value = "禁用/启用租户", notes = "禁用/启用用租户接口")
    public Result updateStatus(@PathVariable("tenantId") Long tenantId, @PathVariable("status") Integer status) {
        SysTenant tenant = tenantService.getById(tenantId);
        if (tenant == null) {
            return Result.fail(ResultCodeEnum.USER_NOT_EXIST);
        }
        tenant.setStatus(status); // 0 禁用 1 启用
        tenantService.updateById(tenant);
        return Result.ok(ResultCodeEnum.SUCCESS);
    }

    // 删除租户
    @DeleteMapping(value = "/{tenantId}")
    @ApiOperation(value = "删除租户", notes = "删除租户接口")
    public Result deleteUser(@PathVariable("tenantId") Long tenantId) {
        SysTenant tenant = tenantService.getById(tenantId);
        if (tenant == null) {
            return Result.fail(ResultCodeEnum.USER_NOT_EXIST);
        }
        tenantService.removeById(tenantId);
        return Result.ok(ResultCodeEnum.SUCCESS);
    }

    // 批量删除租户
    @DeleteMapping(value = "/batch")
    @ApiOperation(value = "批量删除租户", notes = "批量删除租户接口")
    public Result<?> deleteBatch(@RequestBody Long[] tenantIds) {
        for (Long tenantId : tenantIds) {
            SysTenant tenant = tenantService.getById(tenantId);
            if (tenant == null) {
                return Result.fail(ResultCodeEnum.USER_NOT_EXIST);
            }
            tenantService.removeById(tenantId);
        }
        return Result.ok(ResultCodeEnum.SUCCESS);
    }

    // 回收站列表查询
    @GetMapping(value = "/recycle")
    @ApiOperation(value = "回收站列表查询", notes = "回收站列表查询接口")
    public Result<IPage<SysTenantVo>> queryRecycleList(SysTenantModel sysTenantModel, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SysTenantVo> page = new Page<>(pageNo, pageSize);

        return Result.ok(tenantService.queryRecycleList(sysTenantModel, page));
    }


    // 回收站恢复删除租户
    @PutMapping(value = "/recycle/{tenantId}")
    @ApiOperation(value = "回收站恢复删除租户", notes = "回收站恢复删除租户接口")
    public Result restoreUser(@PathVariable("tenantId") Long tenantId) {
        SysTenant tenant = tenantService.getRecycleById(tenantId);
        if (tenant == null) {
            return Result.fail(ResultCodeEnum.USER_NOT_EXIST);
        }
        tenant.setIsDeleted(0);
        tenantService.updateRecycleById(tenant);
        return Result.ok(ResultCodeEnum.SUCCESS);
    }
}
